//
//  LGTUserUtil.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/8.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTUserUtil: NSObject {

    static let share:LGTUserUtil = LGTUserUtil()
    
    //是否是绑定微信
    var bindWXState:Bool = false
    
//    ///  登陆token
//    public var login_token:String = ""
//    // 用户id
//    public var uid:String = ""
//
//    //随机数
//    public var rnd:Int = 0
    
    func saveUserInfo(data:Any){
        //数据存储
        let dataInfo:Dictionary<String, Any> = data as? Dictionary<String, Any> ?? [:]
        let jsonStr:String = BaseUtil.baseUtil.dicValueString(dataInfo) ?? ""
        UserDefaults.standard.set(jsonStr, forKey: "userInfo")
        UserDefaults.standard.synchronize()
    }
    
    func clearUserInfo(){
        //数据存储
        UserDefaults.standard.removeObject(forKey: "userInfo")
        UserDefaults.standard.synchronize()
    }
    
    func saveSumitOrderModel(data:Any){
        //数据存储
        UserDefaults.standard.set(data, forKey: "SumitOrderModel")
        UserDefaults.standard.synchronize()
    }
    
    func clearSumitOrderModel(){
        //数据存储
        UserDefaults.standard.removeObject(forKey: "SumitOrderModel")
        UserDefaults.standard.synchronize()
    }
    
    func getSubmitOrderModel() -> Dictionary<String, Any?>?{
        let sumit:Dictionary<String, Any?>? =  UserDefaults.standard.value(forKey: "SumitOrderModel") as? Dictionary<String, Any?>
        return sumit
    }
    
    func getUserInfo() -> Dictionary<String, Any>?{
        let jsonStr:String = UserDefaults.standard.value(forKey: "userInfo") as? String ?? ""
        if jsonStr.count == 0{
            return nil
        }
        let userInfo:Dictionary<String, Any>? = BaseUtil.baseUtil.getDictionaryFromJSONString(jsonString: jsonStr)
        
//        let userInfo:Dictionary<String, Any>? =  UserDefaults.standard.value(forKey: "userInfo") as? Dictionary<String, Any>
        return userInfo
    }
    
    func updateUserInfo(data:Dictionary<String, Any>){
        //数据存储
        self.saveUserInfo(data: data)
        //用户数据模型初始化
        let userModel:UserModel? = try? lgtDecode(UserModel.self, param: data)
        UserModel.share = userModel
        var tag_id = "tp"
        if userModel?.user_type != 1{
            tag_id = "tr"
        }
        let gt_id = String(format: "%@_%d", tag_id, userModel?.user_id ?? 0)
        GeTuiSdk.bindAlias(gt_id, andSequenceNum: "appstore")
    }
    
    
    //任务主首页弹窗
    func showRWZHomeAlert(){
        UserDefaults.standard.set(true, forKey: "rwzAlert")
        UserDefaults.standard.synchronize()
    }
    
    func getShowRWZHomeAlertState() -> Bool{
        let state:Bool = UserDefaults.standard.value(forKey: "rwzAlert") as? Bool ?? false
        return state
    }
    
    func clearRWZHomeAlert(){
        UserDefaults.standard.set(false, forKey: "rwzAlert")
        UserDefaults.standard.synchronize()
    }
    
    //流量主
    func showLLZHomeAlert(){
        UserDefaults.standard.set(true, forKey: "llzAlert")
        UserDefaults.standard.synchronize()
    }
    
    func getShowLLZHomeAlertState() -> Bool{
        let state:Bool = UserDefaults.standard.value(forKey: "llzAlert") as? Bool ?? false
        return state
    }
    
    func clearLLZHomeAlert(){
        UserDefaults.standard.set(false, forKey: "llzAlert")
        UserDefaults.standard.synchronize()
    }
    
    func setTaskTip(state:Bool){
        UserDefaults.standard.set(state, forKey: "taskTip")
        UserDefaults.standard.synchronize()
    }
    
    func getTaskTip() -> Bool{
        let state:Bool = UserDefaults.standard.value(forKey: "taskTip") as? Bool ?? false
        return state
    }
}
